package drone.basis.assist ;

import java.io.File ;
import java.math.BigDecimal ;
import java.net.URLEncoder ;
import java.util.Date ;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Objects ;

import drone.basis.util.DateUtil ;
import drone.basis.util.ImageUtil ;
import drone.basis.util.NumberUtil ;

public class FileWrapper
{

	private String fileName ;

	private String filePath ;

	private String fileSize ;

	private String fileParent ;

	private String fileHidden ;

	private String fileAbsolutePath ;

	private String fileDirectory ;

	private String fileImage ;

	private String fileWidth ;

	private String fileHeight ;

	private String fileDate ;

	private List < FileWrapper > fileChild = new LinkedList < FileWrapper > ( ) ;

	public static FileWrapper wrapper (
			File file
	)
	{

		FileWrapper fileWrapper = new FileWrapper ( ) ;

		try
		{
			if ( ! Objects.isNull ( file ) )
			{
				fileWrapper.setFileName ( file.getName ( ) ) ;
				fileWrapper.setFilePath ( URLEncoder.encode ( file.getPath ( ) , "UTF-8" ) ) ;
				fileWrapper.setFileSize ( NumberUtil.div ( new BigDecimal ( file.length ( ) ) , new BigDecimal ( 1024 * 1024 ) , 2 ).toPlainString ( ) ) ;
				fileWrapper.setFileParent ( file.getParent ( ) ) ;
				fileWrapper.setFileAbsolutePath ( URLEncoder.encode ( file.getAbsolutePath ( ) , "UTF-8" ) ) ;
				fileWrapper.setFileDirectory ( file.isDirectory ( ) ? "1" : "0" ) ;
				fileWrapper.setFileHidden ( file.isHidden ( ) ? "1" : "0" ) ;
				fileWrapper.setFileDate ( DateUtil.format ( new Date ( file.lastModified ( ) ) ) ) ;

				if ( ImageUtil.isImage ( file ) )
				{
					fileWrapper.setFileImage ( "1" ) ;
					fileWrapper.setFileWidth ( String.valueOf ( ImageUtil.getWidth ( file ) ) ) ;
					fileWrapper.setFileHeight ( String.valueOf ( ImageUtil.getHeight ( file ) ) ) ;
				} else
				{
					fileWrapper.setFileImage ( "0" ) ;
					fileWrapper.setFileWidth ( String.valueOf ( 0 ) ) ;
					fileWrapper.setFileHeight ( String.valueOf ( 0 ) ) ;
				}

				if ( file.isDirectory ( ) )
				{
					for ( File item : file.listFiles ( ) )
					{
						fileWrapper.getFileChild ( ).add ( FileWrapper.wrapper ( item ) ) ;
					}
				}
			}
		} catch ( Exception e )
		{
			e.printStackTrace ( ) ;
		}

		return fileWrapper ;

	}

	public static List < FileWrapper > wrapper (
			List < File > fileList
	)
	{

		List < FileWrapper > wrapperList = new LinkedList < FileWrapper > ( ) ;

		for ( File file : fileList )
		{
			wrapperList.add ( FileWrapper.wrapper ( file ) ) ;
		}

		return wrapperList ;
	}

	public String getFileName ( )
	{
		return fileName ;
	}

	public void setFileName (
			String fileName
	)
	{
		this.fileName = fileName ;
	}

	public String getFilePath ( )
	{
		return filePath ;
	}

	public void setFilePath (
			String filePath
	)
	{
		this.filePath = filePath ;
	}

	public String getFileParent ( )
	{
		return fileParent ;
	}

	public void setFileParent (
			String fileParent
	)
	{
		this.fileParent = fileParent ;
	}

	public String getFileHidden ( )
	{
		return fileHidden ;
	}

	public void setFileHidden (
			String fileHidden
	)
	{
		this.fileHidden = fileHidden ;
	}

	public String getFileAbsolutePath ( )
	{
		return fileAbsolutePath ;
	}

	public void setFileAbsolutePath (
			String fileAbsolutePath
	)
	{
		this.fileAbsolutePath = fileAbsolutePath ;
	}

	public String getFileDirectory ( )
	{
		return fileDirectory ;
	}

	public void setFileDirectory (
			String fileDirectory
	)
	{
		this.fileDirectory = fileDirectory ;
	}

	public List < FileWrapper > getFileChild ( )
	{
		return fileChild ;
	}

	public void setFileChild (
			List < FileWrapper > fileChild
	)
	{
		this.fileChild = fileChild ;
	}

	public String getFileSize ( )
	{
		return fileSize ;
	}

	public void setFileSize (
			String fileSize
	)
	{
		this.fileSize = fileSize ;
	}

	public String getFileDate ( )
	{
		return fileDate ;
	}

	public void setFileDate (
			String fileDate
	)
	{
		this.fileDate = fileDate ;
	}

	public String getFileImage ( )
	{
		return fileImage ;
	}

	public void setFileImage (
			String fileImage
	)
	{
		this.fileImage = fileImage ;
	}

	public String getFileWidth ( )
	{
		return fileWidth ;
	}

	public void setFileWidth (
			String fileWidth
	)
	{
		this.fileWidth = fileWidth ;
	}

	public String getFileHeight ( )
	{
		return fileHeight ;
	}

	public void setFileHeight (
			String fileHeight
	)
	{
		this.fileHeight = fileHeight ;
	}

	public String toString (
			String symbol ,
			String mark
	)
	{
		StringBuffer sb = new StringBuffer ( ) ;

		sb.append ( symbol + " fileName : " + fileName + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " filePath : " + filePath + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileSize : " + fileSize + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileParent : " + fileParent + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileHidden : " + fileHidden + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileAbsolutePath : " + fileAbsolutePath + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileDirectory : " + fileDirectory + System.lineSeparator ( ) ) ;
		sb.append ( symbol + " fileDate : " + fileDate + System.lineSeparator ( ) ) ;
		symbol = symbol + mark ;
		for ( FileWrapper wrapper : fileChild )
		{
			sb.append ( wrapper.toString ( symbol , mark ) ) ;
		}
		return sb.toString ( ) ;
	}

	public static void main (
			String [ ] args
	)
	{
		File file = new File ( "c:\\latch\\drone\\log\\" ) ;

		System.out.println ( FileWrapper.wrapper ( file ).toString ( "" , "-----" ) ) ;
	}

}
